/*
 * 
 * 
 * 
 */
package com.hboxs.asl.dao.impl;

import com.hboxs.asl.Filter;
import com.hboxs.asl.Order;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.dao.ConsultationDao;
import com.hboxs.asl.entity.Consultation;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Product;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * Dao - 咨询
 */
@Repository("consultationDaoImpl")
public class ConsultationDaoImpl extends BaseDaoImpl<Consultation, Long> implements ConsultationDao {

    public List<Consultation> findList(Member member, Product product, Boolean isShow, Integer count, List<Filter> filters, List<Order> orders) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class);
        Root<Consultation> root = criteriaQuery.from(Consultation.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation")));
        if (member != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        }
        if (product != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
        }
        if (isShow != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
        }
        criteriaQuery.where(restrictions);
        return super.findList(criteriaQuery, null, count, filters, orders);
    }

    public Page<Consultation> findPage(Member member, Product product, Boolean isShow, Pageable pageable) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class);
        Root<Consultation> root = criteriaQuery.from(Consultation.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation")));
        if (member != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        }
        if (product != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
        }
        if (isShow != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
        }
        criteriaQuery.where(restrictions);
        return super.findPage(criteriaQuery, pageable);
    }

    public Long count(Member member, Product product, Boolean isShow) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Consultation> criteriaQuery = criteriaBuilder.createQuery(Consultation.class);
        Root<Consultation> root = criteriaQuery.from(Consultation.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forConsultation")));
        if (member != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        }
        if (product != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product));
        }
        if (isShow != null) {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow));
        }
        criteriaQuery.where(restrictions);
        return super.count(criteriaQuery, null);
    }

    @Override
    public List<Consultation> lastThreeConsultations(Product product) {
        String queryStr = "SELECT consultation FROM Consultation As consultation " +
                "WHERE consultation.product=:product " +
                "ORDER BY consultation.createDate DESC ";
        return entityManager.createQuery(queryStr).setParameter("product", product).setMaxResults(3).getResultList();
    }

}